package cc.vv.btpartyjob.partyservice.activity

import android.content.Intent
import android.text.TextUtils
import android.view.View
import android.view.WindowManager
import cc.vv.baselibrary.bean.base.BaseResponseObj
import cc.vv.baselibrary.util.LKTimeUtil
import cc.vv.baselibrary.util.LKToastUtil
import cc.vv.baselibrary.view.BaseTopBarView
import cc.vv.btpartyjob.jnimodule.binder.BaseBinder
import cc.vv.btpartyjob.partyservice.R
import cc.vv.btpartyjob.partyservice.delegate.ClaimWishActivityDelegate
import cc.vv.btpartyjob.partyservice.globle.BroadCastServiceKey
import cc.vv.btpartyjob.partyservice.globle.IntentServiceKey
import cc.vv.btpartyjob.partyservice.module.resp.SubTinyWishResponseObj
import cc.vv.btpartyjob.partyservice.server.DataPartyServer
import cc.vv.mvp.activity.BaseActivityMVP
import cc.vv.mvp.binder.BaseDataBinder

/**
 * 认领别人发布的微心愿
 * Created by Chen.qi on 2018/11/7.
 */
class ClaimWishActivity : BaseActivityMVP<ClaimWishActivityDelegate>() {
    /**
     * 结束时间
     */
    private var currentTime: Long = 0

    private var id: String? = ""

    override fun initData() {
        window.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN)
        viewDelegate?.setTopBarTitle("认领心愿")
        id = intent.getStringExtra(IntentServiceKey.INTENT_KEY_TINY_DETAIL_ID)
        val content = intent.getStringExtra(IntentServiceKey.INTENT_KEY_RL_CONTENT)
        val personName = intent.getStringExtra(IntentServiceKey.INTENT_KEY_RL_PER_NAME)
        viewDelegate?.setContent(content)
        viewDelegate?.setPerson("许愿人：" + personName)
        currentTime = System.currentTimeMillis()
        val formatTime = LKTimeUtil.getInstance().formatTime("yyyy-MM-dd", currentTime)
        viewDelegate?.setCretatTime(formatTime)
    }

    override fun bindEvenListener() {
        viewDelegate?.setTitleBackBtnOnClick(object : BaseTopBarView.EventInterface {
            //返回
            override fun leftOnClick() {
                finish()
            }

            override fun leftCloseClick() {}
            override fun rightOnClick() {
            }
        })


        viewDelegate?.setOnClickListener(View.OnClickListener {
            //提交
            val name = viewDelegate?.getName()
            val tel = viewDelegate?.getMobile()
            val remark = viewDelegate?.getRemark()

            if (TextUtils.isEmpty(name) || TextUtils.isEmpty(tel)) {
                LKToastUtil.showToastShort("请将数据填写完整")
                return@OnClickListener
            }

            DataPartyServer.claimWishHttp(id, name, tel, currentTime, remark, true)
        }, R.id.rl_cwdD_submit)
    }

    override fun getDelegateClass(): Class<ClaimWishActivityDelegate> {
        return ClaimWishActivityDelegate::class.java
    }

    override fun getDataBinder(): BaseDataBinder<*, *> {
        return BaseBinder()
    }


    override fun getData(obj: BaseResponseObj<*>?) {
        super.getData(obj)
        when (obj) {
            is SubTinyWishResponseObj -> {
                sendBroadcast(Intent(BroadCastServiceKey.BROADCAST_REFRESH_OR_FINISH_TINY_WISH))
                finish()
            }
        }
    }
}